#ifndef STRUTTURAICONE_H
#define STRUTTURAICONE_H

#include <QPair>
#include <QMap>
#include <QDateTime>
#include <QtCore>
#include <QtGui>
#include <QWidget>
#include <QTableWidget>
#include <QColor>

#define _DEFAULT_COLOR_ATT_ICON_ \
         QString( "#3456a2")
         
#define _ALTEZZA_PICO_ 20

#define _PICO_VERSION_ "1.4.0"

QMap<int,int> TraduzioneIconeValidate();
QMap<int,int> NumerazioneIconeValidate();

/*
      const T QMap::value ( const Key & key ) const 
      Returns the value associated with the key key. 

      da remoto a mio numero 
		QMap<int,int> trazione = TraduzioneIcone() 
    int numero_mio = trazione.value(numeroremoto);

		  da mio a remoto 
      QMap<int,int> remoti = NumerazioneIcone()
     int numero_remoto = trazione.value(numeromio);

*/




extern inline  QImage GreyScale( QImage income )
{
    
    QImage base =  income.convertToFormat(QImage::Format_RGB32);
    for (int y = 0; y < base.height(); ++y) {
             for (int x = 0; x < base.width(); ++x) {
                 int pixel = base.pixel(x, y);
                 int gray = qGray(pixel);
                 int alpha = qAlpha(pixel);
                 base.setPixel(x, y, qRgba(gray, gray, gray, alpha));
             }
         }
         
    return base;
}

extern inline  QPixmap GreyScalePi( QPixmap income )
{
    QImage resi = GreyScale(income.toImage());
    return QPixmap::fromImage(resi);
    
}

















class IcoLine
{
    public:
    enum { MAGICNUMBER = 0xFFAABBBA, VERSION = 2 };
    IcoLine();
    inline QString categ() { return QString::number(cate); }
    QPixmap erno_pix();
    inline int id() const  { return nummer.first; }
    inline int rid() const { return nummer.second; }
    void update_rid( const int numeroti );
    inline QString Sid() const  { return QString("_%1_").arg(id()); }
    void set_pics( const QPixmap * barcode );
    void set_pics( const QPixmap barcode );
    operator QVariant() const;
    IcoLine operator=( IcoLine d );
    QPixmap pix();
    QPixmap pixbn();
    void close()
    {
       info2 = info.join("|"); 
    }
    void open()
    {
       info = info2.split("|"); 
    }
    
        /* vars permanent */
    QString strinnummer;
    QString it;
    QString de;
    QString fr;
    QString en;
    QStringList info;
    QString info2;
    QChar chars;
    QPixmap pics;
    int cate;
    QPair<int,int> nummer; 
};


Q_DECLARE_METATYPE(IcoLine);


inline QDebug operator<<(QDebug debug, IcoLine& udoc)
{
	debug.nospace() << "IcoLine(name." 
		<< udoc.id() << "," 
    << udoc.it << ","
    << udoc.de << ","
    << udoc.fr << ","
    << udoc.en << ","
    << udoc.info << ","
    << udoc.cate << ","
    << udoc.rid() << ")";
	return debug.space();
}



inline QDataStream& operator<<(QDataStream& out, const IcoLine& udoc)
{
    out << udoc.it;
    out << udoc.de;
    out << udoc.fr;
    out << udoc.en;
    out << udoc.info;
    out << udoc.chars;
    out << udoc.pics;
    out << udoc.cate;
    out << udoc.nummer;
    return out;
}


inline QDataStream& operator>>(QDataStream& in, IcoLine& udoc)
{
    in >> udoc.it;
    in >> udoc.de;
    in >> udoc.fr;
    in >> udoc.en;
    in >> udoc.info;
    in >> udoc.chars;
    in >> udoc.pics;
    in >> udoc.cate;
    in >> udoc.nummer;
    return in;
}







class TabIcone
{
    public:
    enum { MAGICNUMBER = 0xFFAABBFB, VERSION = 3 };
    TabIcone()
    {
    creato = QDateTime::currentDateTime();
    }
    TabIcone operator=( TabIcone d )
    {
      all_lines = d.all_lines;
      creato = d.creato;
      return *this;
    }
    operator QVariant() const {
    return QVariant::fromValue(*this);
    }
     /* vars permanent */
    QList<IcoLine> all_lines;
    QDateTime creato;
};

Q_DECLARE_METATYPE(TabIcone);

/* salvare i codici delle immagini attributi case hotel ecc... */
inline QString SaveTabelicon( TabIcone& data  )
{
	QByteArray bytes;
	QBuffer buffer(&bytes);
	if (!buffer.open(QIODevice::WriteOnly)) {
		return QString();
	}
	QDataStream ds(&buffer);
	/* place header */
	ds.setVersion(QDataStream::Qt_4_0);
	ds << (quint32)TabIcone::MAGICNUMBER;
	ds << (quint32)TabIcone::VERSION;
	ds << data.creato;
	for (int i=0; i<data.all_lines.size(); i++) {
		IcoLine placehere = data.all_lines[i];
		ds << placehere;
	}
	buffer.close();
	return bytes.toBase64();
}

/* decoded base64 stream to put on mysql row , file or network streams */
inline TabIcone OpenTabelicon( const QString datastream_base64   )
{
	QByteArray xcode("");;
	xcode.append(datastream_base64);
	IcoLine set;
	TabIcone data;
	quint32 magic, version;
	QByteArray bytes(QByteArray::fromBase64(xcode));   /* decoded base64 string to QByteArray */
	QBuffer buffer(&bytes);
	if (!buffer.open(QIODevice::ReadOnly)) {
		return data;
	}
	QDataStream ds(&buffer);
	/* place header */
	ds.setVersion(QDataStream::Qt_4_0);
	ds >> magic;
	if ( (quint32)TabIcone::MAGICNUMBER != magic ) {
    qDebug() << "######## MAGICNUMBER not ok  ";
		buffer.close();
		return data;
	}
	ds >> version;
	if ( (quint32)TabIcone::VERSION != version ) {
		qDebug() << "######## VERSION not ok  ";
		buffer.close();
		return data;
	}
	/* place header */
	ds >> data.creato;
	/* fill line qlists */
	while (!ds.atEnd())  {
		ds >> set;
		data.all_lines.append(set);
	}
	buffer.close();
	return data;
}













//
#endif // STRUTTURAICONE_H

